<?php
//$_SERVER['DOCUMENT_ROOT']."/libs/string/
require_once(dirname(__FILE__)."/../../libs/string/libstr.php");
$mc=new libstr();
class pager  {

	public $isre=false;
	public $htmltp=".html";
	public $mycpage="page";
	public $lchar="[";
	public $rchar="]";
    public $indexno=1;
	function  __construct()
	{
		global $rewrite;
		global $rewritefile;
		$this->isre=$rewrite;
		$this->htmltp=$rewritefile;
		$this->mycpage="page";
	}
	function fp($mypath, $Totalcount, $Pagesize, $tgg, $str)//*
	{

		return $this->nextpages($mypath,$Totalcount, $Pagesize, $tgg, $str, "", "", "", "fp");
	}
	function pp($mypath, $Totalcount, $Pagesize, $tgg, $str)//*
	{
		return $this->nextpages($mypath,$Totalcount, $Pagesize, $tgg, "", $str, "", "", "pp");
	}
	function np($mypath, $Totalcount, $Pagesize, $tgg, $str)//*
	{
		return $this->nextpages($mypath,$Totalcount, $Pagesize, $tgg, "", "", $str, "", "np");
	}
	function lp($mypath, $Totalcount, $Pagesize, $tgg, $str)//*
	{
		return $this->nextpages($mypath,$Totalcount, $Pagesize, $tgg, "", "", "", $str, "lp");
	}
	function startid($mypath, $Totalcount, $Pagesize)//*
	{
		return $this->nextpages("",$Totalcount, $Pagesize, "", "", "", "", "", "startid");
	}
	function endid($mypath, $Totalcount, $Pagesize)//*
	{
		return $this->nextpages("",$Totalcount, $Pagesize, "", "", "", "", "", "Endid");
	}
	function pagecount($mypath, $Totalcount, $Pagesize)//*
	{
		return $this->nextpages("",$Totalcount, $Pagesize, "", "", "", "", "", "Pagecount");
	}
	function spage($mypath, $Totalcount, $Pagesize)//*
	{
		return $this->nextpages("",$Totalcount, $Pagesize, "", "", "", "", "", "page");
	}
	function check_str($str, $substr)
	{
		for ( $i=0; $i<strlen($str); $i++ )
		{
			if ($substr == $str[$i])
			{
				return "true";
			}
			else
			{
				return "false";
			}
		}
	}

	function cs($str)//处理URL参数的函数
	{

		$ul="";
		foreach($_GET  as $sForm => $value)
		{
			$ul.="&".$sForm ."=".urlencode($value);
		}
		if($ul!="")
		{
			$ul="?".substr($ul,1,strlen($ul));
		}
		$urll=$_SERVER['PHP_SELF'].$ul;
		//$urll=str_replace($this->webphp(),".php",$_SERVER["REQUEST_URI"]);


		$page = "";
		$page =str_replace("?","",str_replace($_SERVER['PHP_SELF'],"",$urll));

		$cstext =  str_replace($str . "=" . $_GET[$str],"",$page);
		//	$cstext =   preg_replace($str . "=" . $_GET[$str],"",$page);
		if ((eregi("&",$page)!=1) && (strlen($cstext) == 0))
		{

		}
		else
		{
			$cstext = $cstext . "&";
		}
		//$cstext = preg_replace("/&&/i","&",$cstext);
		$cstext =  str_replace("&&" , "&",$cstext);
		$cstext = $cstext . $str . "=";
		return $cstext;
	}
	function cs_h($str)//处理URL参数的函数
	{

		$ul="";
		foreach($_GET  as $sForm => $value)
		{
			$ul.="&".$sForm ."=".urlencode($value);
		}
		if($ul!="")
		{
			$ul="?".substr($ul,1,strlen($ul));
		}
		$urll=$_SERVER['PHP_SELF'].$ul;
		//$urll=str_replace($this->webphp(),".php",$_SERVER["REQUEST_URI"]);


		$page = "";
		$page =str_replace("?","",str_replace($_SERVER['PHP_SELF'],"",$urll));

		$cstext =  str_replace($str . "=" . $_GET[$str],"",$page);
		//	$cstext =   preg_replace($str . "=" . $_GET[$str],"",$page);

		if ((eregi("&",$page)!=1) && (strlen($cstext) == 0))
		{

		}
		else
		{
			$cstext = $cstext . "&";
		}
		//$cstext = preg_replace("/&&/i","&",$cstext);
		$cstext =  str_replace("&&" , "&",$cstext);
		$cstext = $cstext . $str . "=";
		return $cstext;
	}
	function getreurl($str,$id)
	{
		$tx=split('=',$str);
		return  $tx[$id];
	}
	function reurl($str)
	{
		if($this->isre==false)
		{
			return $str;
		}
		//$str=str_replace(".php?&","_",$str);
		//$str=str_replace(".php?","_",$str);
		$str=split("\?",$str);
		$str1=$str[0];
		$ul=split("&",$str[1]);
		$tx="";
		$i=0;
		foreach($ul as $value)
		{
			$tx.="-". $this->getreurl($value,1);
			$i++;
		}
		//$str=str_replace("&","_",$str);
		//$str=str_replace("=","_",$str);
		$strtx=$str1.$tx.$this->htmltp;
		$strtx=str_replace(".php","",$strtx);

		return $strtx;


	}
	public function showtiao($tcount, $pagez, $page, $weizhi)
	{
		$tiao = 0;
		if ($page == 0)
		{
			$page = 1;
		}
		if ($tcount / $pagez < 1)
		{
			if ($weizhi == "q")
			{
				if ($tcount == 0)
				{
					$tiao = 0;
				}
				else
				{
					$tiao = 1;
				}
			}
			else
			{
				if ($tcount == 0)
				{
					$tiao = 0;
				}
				else
				{
					$tiao = $tcount;
				}
			}
		}
		else
		{
			if ($tcount % $pagez > 0)
			{
				if ($weizhi == "q")
				{
					$tiao = (($page - 1) * $pagez) + 1;
				}
				else
				{
					if ($tcount / $pagez < $page)
					{
						$tiao = (($page - 1) * $pagez) + ($tcount % $pagez);
					}
					else
					{
						$tiao = (($page) * $pagez);
					}
				}
			}
			else
			{
				if ($weizhi == "q")
				{
					$tiao = (($page - 1) * $pagez) + 1;
				}
				else
				{
					$tiao = (($page) * $pagez);
				}
			}
		}
		return $tiao;
	}
	function  nextpages($mypath,$Totalcount,$Pagesize,$tgg,$fp, $pp,$np,$lp,$kind)
	{
		global $mc;
		$Pagecount=0;
		$Page = 1;



		if (($Totalcount % $Pagesize) == 0)
		{
			$Pagecount = $Totalcount / $Pagesize;
		}
		else
		{
			$Pagecount = floor($Totalcount / $Pagesize) + 1;
		}

		if ($mc->Ismp($_GET[$this->mycpage])=="")
		{
			$Page = 1;
		}
		else
		{

			$Page = $_GET[$this->mycpage];


		}
		if ($Page < 1 || $Page > $Pagecount)
		{
			$Page = 1;
			// Response.Write("You must input munber");
		}
		$Startid = ($Page - 1) * $Pagesize;
		$Startid1 = $Startid + 1;
		$this->indexno=$Startid1;
		if ($Totalcount == 0) { $Startid1 = 0; }
		$Endid = $Startid + $Pagesize;
		if ($Page * $Pagesize >= $Totalcount) { $Endid = $Totalcount; }

		if ($kind == "Pagecount") { return $Pagecount; }
		if ($kind == "Totalcount") { return $Totalcount; }
		if ($kind == "page") { return $Page; }
		if ($kind == "startid") { return $Startid1; }
		if ($kind == "Endid") { return $Endid; }


		$firstpage="";
		$perpage="";
		$nextpage="";
		$lastpage="";
		$_firstpage="";
		$_perpage="";
		$_nextpage = "";
		$_lastpage = "";

		if ($kind == "fp")
		{
			$_firstpage = "首页[split]首页";
			if ($fp != "") { $_firstpage = $fp; }
		}
		if ($kind == "pp")
		{
			$_perpage = "上页[split]上页";
			if ($pp != "") { $_perpage = $pp; }
		}
		if ($kind == "np")
		{
			$_nextpage = "下页[split]下页";
			if ($np != "") { $_nextpage = $np; }
		}
		if ($kind == "lp")
		{
			$_lastpage = "末页[split]末页";
			if ($lp != "") { $_lastpage = $lp; }
		}
		if ($Page > 1)
		{
			$rooturl=$this->reurl($mypath . "?" . $this->cs($this->mycpage) . "1");
			$firstpage = "<a href='".$rooturl."' target='" . $tgg . "'>" . $this->pagel($_firstpage, 0) . "</a>";
			$rooturl=$this->reurl( $mypath . "?" . $this->cs($this->mycpage) . ($Page - 1));
			$perpage = "<a href='".$rooturl."' target='" . $tgg . "'>" . $this->pagel($_perpage, 0) . "</a>";
		}
		else
		{
			$firstpage = $this->pagel($_firstpage, 1);
			$perpage = $this->pagel($_perpage, 1);

		}

		if ($Page < $Pagecount)
		{
			$rooturl=$this->reurl( $mypath . "?" . $this->cs($this->mycpage) . ($Page + 1));
			$nextpage = "<a href='".$rooturl."' target='" . $tgg . "'>" . $this->pagel($_nextpage, 0) . "</a>";
			$rooturl=$this->reurl($mypath . "?" . $this->cs($this->mycpage) . ($Pagecount));
			$lastpage = "<a href='".$rooturl."' target='" . $tgg . "'>" . $this->pagel($_lastpage, 0) . "</a>";
		}
		else
		{
			$nextpage = $this->pagel($_nextpage, 1);
			$lastpage = $this->pagel($_lastpage, 1);

			//sye = "<a href='" + Request.CurrentExecutionFilePath + "?" + $this->cs($this->mycpage) + Convert.ToString(page) + "' target='" + $tgg + "'>" + Pagetext1 + "</a>";
		}
		if ($kind == "fp") { return $firstpage; }
		if ($kind == "pp") { return $perpage; }
		if ($kind == "np") { return $nextpage; }
		if ($kind == "lp") { return $lastpage; }
		if ($kind == "all") { return $firstpage . "  " . $perpage . "  " . $nextpage . "  " . $lastpage; }
		return "error!";

		// return $firstpage + $perpage + $nextpage + $lastpage + "显示" + $Startid1 + "-" + $Endid + "第" + Page + "页/共" + $Pagecount + "页" + "总" + $Totalcount + "条";
	}
	function pagel($str, $ii)
	{


		if (eregi("\[split\]",$str) != 1) { $str = $str . "[split]" . $str; }
		$mm = split("\[split\]",$str);
		if (count($mm) < 1 && $ii > 0) { return $mm[0]; }
		return $mm[$ii];

	}
	function gopage($mypath, $text1,$st1, $text2, $st2)//*
	{
		global $mc;
		$CurrentPage = 1;
		if ($mc->Ismp($_GET[$this->mycpage]) != "")
		{
			$CurrentPage =$_GET[$this->mycpage];
		}
		$js = "";
		$ts = split("\*",$text1);
		$js .= "<script language=\"JavaScript\">\r";
		$js .= " function getxq_gogo()\r";
		$js .= " {\r";
		//$js .= "document.getElementById (\"xq_gopage\").value=\"\";\r";
		if($this->isre==false)
		{
			$rooturl=$this->reurl( $mypath . "?" . $this->cs($this->mycpage));
			$js .= "location.href='" .$rooturl . "'+document.getElementById (\"xq_gopage\").value;\r";
		}else
		{
			$rooturl=$this->reurl( $mypath . "?" . $this->cs($this->mycpage));
			$rooturl=str_replace($this->htmltp,"",$rooturl)."'+document.getElementById (\"xq_gopage\").value+'".$this->htmltp."'";

			$js .= "location.href='" .$rooturl . ";\r";

		}
		$js .= " }\r";
		$js .= "</script>";
		$js .= $ts[0] . "<input name=\"xq_gopage\" type=\"text\" id=\"xq_gopage\" value=\"" . $CurrentPage . "\" style=\"" . $st1 . "\">" . $ts[1];
		$js .= "<input name=\"xq_gosim\" type=\"button\" id=\"xq_gosim\" value=\"" . $text2 . "\" style=\"" . $st2 . "\" onClick=\"getxq_gogo();\">";
		return $js;
	}
	function fstr( $mypath, $Totalcount, $Pagesize, $sd,$tgg)//*
	{
		global $mc;
		$urlstr = $this->cs($this->mycpage);
		$myPageCount=0;
		if (($Totalcount % $Pagesize) == 0)
		{
			$myPageCount = $Totalcount / $Pagesize;
		}
		else
		{
			$myPageCount = ($Totalcount / $Pagesize) + 1;
		}
		$i_rs1;
		$si;
		$ei;
		$kv;
		$myipagecurrent = 1;
		if ($mc->Ismp($_GET[$this->mycpage]) == "")
		{
			$myipagecurrent = 1;
		}
		else
		{

			$myipagecurrent = $_GET[$this->mycpage];
		}
		$i_rs1 = 0;
		$si = 0;
		$ei = 0;
		$kv = 0;

		if ($myPageCount <= $sd)
		{
			$si = 1;
			$ei = $myPageCount;
		}
		if ($myPageCount > $sd)
		{
			if ($mc->Ismp($_GET[$this->mycpage]) == "")
			{
				$si = 1;
				$ei = $sd;
			}
			else
			{
				if (($myipagecurrent) >= ($sd))
				{
					$si = ($sd - 2) * (($myipagecurrent) / ($sd - 2)) - 1;
					$ei = $si + ($sd - 2) + 1;
					if ($ei >= $myPageCount)
					{
						$ei = $myPageCount;
					}
					if (($myipagecurrent) == ($si - 2))
					{
						$si = ($sd - 2) * ((($myipagecurrent) / ($sd - 2) - 1)) - 1;
						$ei = $si + ($sd - 2) + 1;
						if ($ei >= $myPageCount)
						{
							$ei = $myPageCount;
						}
					}
				}
				if (($myipagecurrent) < ($sd))
				{
					$si = 1;
					$ei = $sd;
				}
			}


		}
		$shuzi12="";
		$km="";
		$opage="";
		$km = "";
		if ($urlstr != "")
		{
			$urlstr = $urlstr . "&";
		}
		if (($myPageCount) > ($sd))
		{
			if (($myPageCount) != ($ei))
			{
				$rooturl=$this->reurl( $mypath . "?" . $this->cs($this->mycpage) . $myPageCount );
				$km = "... ".$this->lchar."<a href=\"" .$rooturl . "\" target='" . $tgg . "'>" . $myPageCount . "</a>".$this->rchar."";
			}
		}
		$shuzi12 = "";
		for ($i_rs1 = $si; $i_rs1 <= $ei; $i_rs1++)
		{
			$rooturl=$this->reurl($mypath . "?" . $this->cs($this->mycpage) . $i_rs1  );
			$shuzi12 = $shuzi12 . "".$this->lchar."<a href=\"" .$rooturl. "\" target='" . $tgg . "'>";
			$opage = "1";
			// if ($myipagecurrent != "")
			// {
			$opage = $myipagecurrent;
			// }
			if (($opage) == ($i_rs1))
			{
				$shuzi12 = $shuzi12 . "<font ";
				$shuzi12 = $shuzi12 . "color='red'";
				$shuzi12 = $shuzi12 . ">";
			}
			$shuzi12 = $shuzi12 . $i_rs1 . "";
			if (($opage) == ($i_rs1))
			{
				$shuzi12 = $shuzi12 . "</font>";
			}
			$shuzi12 = $shuzi12 . "</a>".$this->rchar."";
		}
		// return $shuzi12 . $km;
		return $shuzi12;
	}

	//	实行文章分页
	function  newscontentpager($content,$index=1)
	{
		if($index=="")
		{
			$index=1;
		}
		if($index<1)
		{
			$index=1;
		}
		$tx=split("\[spager\]",$content);
		return $tx[$index-1];
	}
	function getnewspagercount($content)
	{
		$tx=split("\[spager\]",$content);
		return count($tx);
	}
	function newscontentinsert($content,$size=2000)
	{
		global $mc;
		$tx=trim($mc->rehtml($content));
		$txcount=strlen($tx);
		if($txcount<=$size)
		{
			return $content;
		}
		if($size==0)
		{
			return $content;
		}
		$pageno=$txcount/$size;

		if($txcount % $size !=0)
		{
			$pageno=$pageno+1;
		}
		$mytxt="";
		$retxt="";
		for ($i=1;$i<$pageno;$i++)
		{

			// $txs = substr($content,(($i-1)*$size),$size)."[spager]";
			//$txs=$this->str_insert($content,($i*$size),"[spager]");
			//	$txs=$this->str_insert($txs,($i*$size)+8,"[spager]");
			$txs1=$mc->cut_str($content,($i-1)*$size,"");
			$txs=$mc->cut_str($content,$i*$size,"");
			$mytxt= str_replace($txs1,"",$txs );
			if($mytxt !="")
			{
				$retxt.=$mytxt."[spager]";
			}
		}
		return  substr($retxt,0,strlen($retxt)-8);
	}
	function  fsarr($str)
	{
		$arr=array();
		$tx=split("\]",$str);
		$i=$ik;
		foreach ($tx as $rs)
		{
			if($rs=="")continue;
			$arr[$i]["index"]="1";
			$arr[$i]["color"]="false";
			$tx1=split("target",$rs);
			$tx1= $tx1[0];
			$tx1=str_replace("\"","",$tx1);
			$tx1=str_replace("[<a href=","",$tx1);

			$arr[$i]["url"]=trim($tx1);
			$k3000=split("&",$tx1);
			foreach ($k3000 as $mvb){
				if(trim($mvb)!=""){
					if(ereg("page=",$mvb)==1){
                      $arr[$i]["index"]=str_replace("page=","",$mvb);
                      $arr[$i]["index"]=str_replace("?","",$arr[$i]["index"]);
                      if($arr[$i]["index"]==""){
                      	$arr[$i]["index"]="1";
                      }
					}
				}
			}
			if(ereg("font", $rs)==1)
			{
				$arr[$i]["color"]="true";
			}
			$i++;
		}
		return $arr;
	}
	function  next($str)
	{
		if(ereg("target",$str)!=1)
		{
			return "javascript:;";
		}
		$tx1=split("target",$str);
		$tx1= $tx1[0];
		$tx1=str_replace("\"","",$tx1);
		$tx1=str_replace("'","",$tx1);
		$tx1=str_replace("<a href=","",$tx1);
		return trim($tx1);
	}
}

//$pgg=new pager();
//$pgg->newscontentinsert("您好<br>，你叫什么名sss字呢，我叫谢谢您，你的吗",3);
?>